package com.bjsxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.Market;
import com.bjsxt.model.Result;
import com.bjsxt.service.MarketService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * description:
 * 交易对配置信息控制层
 *
 * @author ALIENWARE
 * @version v1.0.0
 * @create 05/29/2024
 **/
@RestController
@RequestMapping("/markets")
@Api(tags = "交易对配置信息控制器")
public class MarketController {
    /**
     * 交易对配置信息业务层
     */
    @Autowired
    private MarketService marketService;

    /**
     * 分页查询交易对配置信息数据
     *
     * @param page        分页对象<Market>
     * @param tradeAreaId 交易区域ID
     * @param status      状态,0禁用,1启用
     * @return 统一响应对象
     */
    @GetMapping
    @PreAuthorize("hasAuthority('trade_market_query')")
    @ApiOperation(value = "分页查询配置信息数据", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "Long", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "size", value = "每页条数", dataType = "Long", paramType = "query", example = "20"),
            @ApiImplicitParam(name = "tradeAreaId", value = "交易区域ID", dataType = "Long", paramType = "query", example = "10012455236551"),
            @ApiImplicitParam(name = "status", value = "状态,0禁用,1启用", dataType = "Byte", paramType = "query", example = "1")
    })
    public Result<Page<Market>> findByPage(@ApiIgnore Page<Market> page, Long tradeAreaId, Byte status) {
        // 调用业务层进行分页数据的查询
        Page<Market> marketPage = marketService.pageByTradeAreaIdAndStatus(page, tradeAreaId, status);
        // 返回响应数据
        return Result.success(marketPage);
    }

    /**
     * 获取全部的交易对配置信息数据
     *
     * @return 统一响应对象
     */
    @GetMapping("/all")
    // @PreAuthorize("hasAuthority('trade_market_query')")
    @ApiOperation(value = "获取全部的交易对配置信息数据", authorizations = {@Authorization("Authorization")})
    public Result<List<Market>> getAll() {
        // 调用业务层查询所有的币种
        List<Market> markets = marketService.list();
        // 返回响应数据
        return Result.success(markets);
    }

    /**
     * 增加一个交易对配置信息
     *
     * @param market 交易对配置信息管理对象
     * @return 统一响应对象
     */
    @PostMapping
    @PreAuthorize("hasAuthority('trade_market_create')")
    @ApiOperation(value = "增加一个交易对配置信息", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market", value = "Market 的 json 数据", dataType = "String", paramType = "body", example = "{}"),
    })
    public Result<Void> add(@RequestBody @Validated Market market) {
        // 调用业务层方法进行数据的保存
        boolean flag = marketService.save(market);
        // 如果为 true 新增成功
        if (flag) return Result.success("新增成功");
        // 如果为 false 新增失败
        return Result.fail("新增失败");
    }

    /**
     * 修改一个交易对配置信息
     *
     * @param market 交易对配置信息管理对象
     * @return 统一响应对象
     */
    @PatchMapping
    @PreAuthorize("hasAuthority('trade_market_update')")
    @ApiOperation(value = "修改一个交易对配置信息", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market", value = "Market 的 json 数据", dataType = "String", paramType = "body", example = "{}"),
    })
    public Result<Void> update(@RequestBody @Validated Market market) {
        // 调用业务层方法进行数据的保存
        boolean flag = marketService.updateById(market);
        // 如果为 true 更新成功
        if (flag) return Result.success("更新成功");
        // 如果为 false 更新失败
        return Result.fail("更新失败");
    }

    /**
     * 启用/禁用一个交易对配置信息
     *
     * @param id     要启用/禁用交易对配置信息 Id
     * @param status 要设置的交易对配置信息状态值 1: 启用 0: 禁用
     * @return 统一响应对象
     */
    @PostMapping("/updateStatus")
    @PreAuthorize("hasAuthority('trade_market_update')")
    @ApiOperation(value = "启用/禁用一个交易对配置信息", authorizations = {@Authorization("Authorization")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要启用/禁用交易对配置信息 Id", dataType = "Long", required = true, paramType = "query", example = "1"),
            @ApiImplicitParam(name = "status", value = "要设置的交易对配置信息状态值 1: 启用 0: 禁用", dataType = "Byte", paramType = "query", example = "0")
    })
    public Result<Void> updateStatus(@NotNull Long id, Byte status) {
        // 创建更新的 tradeArea 对象
        Market market = new Market();
        // 设置 id 及需要更新的值
        market.setId(id);
        market.setStatus(status);
        // 调用业务层方法进行数据的保存
        boolean flag = marketService.updateById(market);
        // 如果为 true 更新成功
        if (flag) return Result.success("更新成功");
        // 如果为 false 更新失败
        return Result.fail("更新失败");
    }
}
